package com.future;

/**
 * Description: 221. 最大正方形
 *
 * @author weiruibai.vendor
 * Date: 2022/7/6 14:39
 */
public class Solution_221 {

    public static void main(String[] args) {
        char[][] grid = new char[][]{
                {'1', '1', '1', '1', '1', '0'},
                {'1', '0', '0', '1', '0', '1'},
                {'1', '0', '1', '1', '1', '1'},
                {'0', '1', '1', '1', '1', '1'}};
        grid = new char[][]{
                {'1', '1', '1', '1', '1', '1', '1', '1'},
                {'1', '1', '1', '1', '0', '1', '1', '0'},
                {'1', '1', '0', '1', '1', '1', '1', '0'},
                {'1', '1', '1', '1', '1', '0', '0', '0'},
                {'1', '1', '1', '1', '1', '0', '0', '0'}};
        /*grid = new char[][]{
                {'1', '1', '0', '1'},
                {'1', '1', '0', '1'},
                {'1', '1', '1', '1'}
        };*/
       /* grid = new char[][]{
                {'0', '1', '1', '1'},
                {'1', '1', '1', '1'},
                {'0', '1', '1', '1'}
        };*/
       /* grid = new char[][]{
                {'0', '0', '0', '1', '0', '1', '0'},
                {'0', '1', '0', '0', '0', '0', '0'},
                {'0', '1', '0', '1', '0', '0', '1'},
                {'0', '0', '1', '1', '0', '0', '1'},
                {'1', '1', '1', '1', '1', '1', '0'},
                {'1', '0', '0', '1', '0', '1', '1'},
                {'0', '1', '0', '0', '1', '0', '1'},
                {'1', '1', '0', '1', '1', '1', '0'},
                {'1', '0', '1', '0', '1', '0', '1'},
                {'1', '1', '1', '0', '0', '0', '0'}
        };*/
        System.out.println(maximalSquare(grid));
    }

    public static int maximalSquare(char[][] matrix) {
        int N = matrix.length;
        int M = matrix[0].length;
        int[][] rightArr = new int[N][M];
        for (int i = 0; i < N; i++) {
            rightArr[i][M - 1] = matrix[i][M - 1] == '1' ? 1 : 0;
            for (int j = M - 2; j >= 0; j--) {
                rightArr[i][j] = matrix[i][j] == '1' ? rightArr[i][j + 1] + 1 : 0;
            }
        }
        int[][] downArr = new int[N][M];
        for (int i = 0; i < M; i++) {
            downArr[N - 1][i] = matrix[N - 1][i] == '1' ? 1 : 0;
            for (int j = N - 2; j >= 0; j--) {
                downArr[j][i] = matrix[j][i] == '1' ? downArr[j + 1][i] + 1 : 0;
            }
        }
        // start
        int ans = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (matrix[i][j] == '1') {
                    /**
                     * (i,j)        (i,j_right)
                     * (i_down,j)   (i_down,j_right)
                     */
                    int j_right = rightArr[i][j];
                    int i_down = downArr[i][j];
                    int border = Math.min(i_down, j_right);
                    for (int k = i; k < i + border; k++) {
                        if (border > rightArr[k][j]) {
                            border = rightArr[k][j];
                        }
                    }
                    ans = Math.max(ans, border * border);
                }
            }
        }
        return ans;
    }

}
